package com.lefu.es.util;

import android.content.Context;
import android.text.TextUtils;
import android.util.Log;
import android.view.View;
import android.widget.TextView;

import com.holtek.libHTBodyfat.HTBodyfatGeneral;
import com.holtek.libHTBodyfat.HTDataType;
import com.lefu.es.constant.UtilConstants;
import com.lefu.es.entity.Records;
import com.lefu.es.entity.UserModel;
import com.lefu.es.service.RecordService;
import com.lefu.es.view.MyTextView5;

import java.math.BigDecimal;
import java.util.Date;
import java.util.Locale;

/**
 * 工具类
 *
 * @author Leon 2015-11-19
 */
public class MyUtil {
    private static final String TAG = "MyUtil";
    private static Records lastRecod;

    public static String getCurrentDate() {
        StringBuffer sb = new StringBuffer();
        long date = System.currentTimeMillis();
        sb.append("f1");
        sb.append(Integer.toHexString(Integer.parseInt(DateUtil.getYear(date).substring(0, 2))));
        int year2 = Integer.parseInt(DateUtil.getYear(date).substring(2, 4));
        if (year2 < 10) {
            sb.append("0");
        }
        sb.append(Integer.toHexString(year2));

        int month = Integer.parseInt(DateUtil.getMonth(date));
        if (month < 10) {
            sb.append("0");
        }
        sb.append(Integer.toHexString(month));
        int day = Integer.parseInt(DateUtil.getDay(date));
        if (day < 10) {
            sb.append("0");
        }
        sb.append(Integer.toHexString(day));
        int hour = Integer.parseInt(DateUtil.getHour(date));
        if (hour < 10) {
            sb.append("0");
        }
        sb.append(Integer.toHexString(hour));
        int minute = Integer.parseInt(DateUtil.getMinute(date));
        if (minute < 10) {
            sb.append("0");
        }
        sb.append(Integer.toHexString(minute));
        int second = Integer.parseInt(DateUtil.getSecond(date));
        if (second < 10) {
            sb.append("0");
        }
        sb.append(Integer.toHexString(second));
        System.out.println("sync current date: " + sb.toString());
//		sb.append(Integer.toHexString(20));
//		sb.append(Integer.toHexString(16));
//		sb.append(Integer.toHexString(03));
//		sb.append(Integer.toHexString(01));
//		sb.append(Integer.toHexString(12));
//		sb.append(Integer.toHexString(37));
//		sb.append(Integer.toHexString(48));
        return sb.toString();
    }

    /**
     * 获取下发信息
     */
    public static String getUserInfo() {
        UserModel userModel = UtilConstants.CURRENT_USER;
        if (userModel == null) {
            return null;
        }
        StringBuffer sb = new StringBuffer();
        sb.append(userModel.getGroup().replace("P", "0"));
        sb.append("0").append(userModel.getSex());
        sb.append("0").append(userModel.getLevel());
        int height = (int) userModel.getBheigth();
        if (height > 15) {
            sb.append(Integer.toHexString(height));
        } else {
            sb.append("0" + Integer.toHexString(height));
        }

        int age = userModel.getAgeYear();
        if (age > 15) {
            sb.append(Integer.toHexString(age));
        } else {
            sb.append("0" + Integer.toHexString(age));
        }

        if (userModel.getDanwei().equals(UtilConstants.UNIT_KG)) {
            sb.append("01");
        } else if (userModel.getDanwei().equals(UtilConstants.UNIT_LB)) {
            sb.append("02");
        } else if (userModel.getDanwei().equals(UtilConstants.UNIT_ST)) {
            sb.append("04");
        } else if (userModel.getDanwei().equals(UtilConstants.UNIT_FATLB)) {
            sb.append("02");
        } else {
            sb.append("01");
        }

        String bcc = StringUtils.getBCC(StringUtils.hexStringToByteArray(sb.toString()));
        String out = "FE" + sb.toString() + bcc;
        return out;
    }

    public static String getUserInfo(UserModel userModel) {
        if (userModel == null) {
            return null;
        }
        StringBuffer sb = new StringBuffer();
        sb.append(userModel.getGroup().replace("P", "0"));
        sb.append("0").append(userModel.getSex());
        sb.append("0").append(userModel.getLevel());
        int height = (int) userModel.getBheigth();
        if (height > 15) {
            sb.append(Integer.toHexString(height));
        } else {
            sb.append("0" + Integer.toHexString(height));
        }

        int age = userModel.getAgeYear();
        if (age > 15) {
            sb.append(Integer.toHexString(age));
        } else {
            sb.append("0" + Integer.toHexString(age));
        }

        if (userModel.getDanwei().equals(UtilConstants.UNIT_KG)) {
            sb.append("01");
        } else if (userModel.getDanwei().equals(UtilConstants.UNIT_LB)) {
            sb.append("02");
        } else if (userModel.getDanwei().equals(UtilConstants.UNIT_ST)) {
            sb.append("04");
        } else if (userModel.getDanwei().equals(UtilConstants.UNIT_FATLB)) {
            sb.append("02");
        } else {
            sb.append("01");
        }

        String bcc = StringUtils.getBCC(StringUtils.hexStringToByteArray(sb.toString()));
        String out = "FE" + sb.toString() + bcc;
        return out;
    }

    /**
     * 解析报文
     */
    public static Records parseMessage(RecordService recordService, String readMessage) {
        LogUtil.e("****旧称解析报文-->" + recordService.toString());
        Records record = new Records();
        try {
            record.setRecordTime(UtilTooth.dateTimeChange(new Date()));
            record.setScaleType(readMessage.substring(0, 2));
            record.setUgroup("P" + StringUtils.hexToTen(readMessage.substring(3, 4)) + "");
            record.setLevel(StringUtils.hexToTen(readMessage.substring(2, 3)) + "");
            String biary = StringUtils.hexToBirary(readMessage.substring(4, 6));
            if (biary.length() < 8) {
                for (int i = biary.length(); i < 8; i++) {
                    biary = "0" + biary;
                }
            }
            record.setSex(biary.substring(0, 1));
            record.setsAge(StringUtils.binaryToTen(biary.substring(1)) + "");
            record.setsHeight(StringUtils.hexToTen(readMessage.substring(6, 8)) + "");
            int weight = StringUtils.hexToTen(readMessage.substring(8, 12));
            record.setSweight((weight * 0.1) + "");
            String unit = readMessage.substring(12, 14);
            record.setSbodyfat((StringUtils.hexToTen(readMessage.substring(12, 16)) * 0.1) + "");
            record.setSbone((StringUtils.hexToTen(readMessage.substring(16, 18)) * 0.1) + "");
            record.setSmuscle((StringUtils.hexToTen(readMessage.substring(18, 22)) * 0.1) + "");
            record.setSvisceralfat(StringUtils.hexToTen(readMessage.substring(22, 24)) + "");
            record.setSbodywater((StringUtils.hexToTen(readMessage.substring(24, 28)) * 0.1) + "");
            record.setSbmr((StringUtils.hexToTen(readMessage.substring(28, 32)) * 1) + "");
            if (readMessage.length() > 32)
                record.setBodyAge(StringUtils.hexToTen(readMessage.substring(32, 34)) * 1);
            record.setRbmr(StringUtils.isNumber(record.getSbmr()) == true ? (Integer.parseInt(record.getSbmr()) / 1) : 0);
            record.setRbodyfat(StringUtils.isNumber(record.getSbodyfat()) == true ? Float.parseFloat(record.getSbodyfat()) : 0);
            record.setRbodywater(StringUtils.isNumber(record.getSbodywater()) == true ? Float.parseFloat(record.getSbodywater()) : 0);
            record.setRbone(StringUtils.isNumber(record.getSbone()) == true ? Float.parseFloat(record.getSbone()) : 0);
            record.setRmuscle(StringUtils.isNumber(record.getSmuscle()) == true ? Float.parseFloat(record.getSmuscle()) : 0);
            record.setRvisceralfat(StringUtils.isNumber(record.getSvisceralfat()) == true ? (Integer.parseInt(record.getSvisceralfat()) / 1) : 0);
            record.setRweight(StringUtils.isNumber(record.getSweight()) == true ? Float.parseFloat(record.getSweight()) : 0);
            if (UtilConstants.BABY_SCALE.equals(record.getScaleType())) {
                if (unit.equals("01")) {
                    if (null != UtilConstants.CURRENT_USER)
                        UtilConstants.CURRENT_USER.setDanwei(UtilConstants.UNIT_FATLB);
                } else {
                    if (null != UtilConstants.CURRENT_USER)
                        UtilConstants.CURRENT_USER.setDanwei(UtilConstants.UNIT_KG);
                }
                record.setUnitType(StringUtils.hexToTen(unit));
                record.setRweight(UtilTooth.myround2((float) (record.getRweight() * 0.1)));
            } else if (UtilConstants.KITCHEN_SCALE.equals(record.getScaleType())) { //单位是g
                record.setSweight(weight + "");
                record.setRweight(Float.parseFloat(record.getSweight()));
                record.setUnitType(StringUtils.hexToTen(readMessage.substring(12, 14)));
            } else {
                if("lb".equals(UtilConstants.CURRENT_USER.getDanwei())){
                    record.setUnitType(1);
                }else{
                    record.setUnitType(0);
                }

            }
            if (UtilConstants.KITCHEN_SCALE.equals(record.getScaleType())) {
                record.setSbmi("0.0");
                record.setRbmi(0.0f);
            } else {
                if (null != UtilConstants.CURRENT_USER) {
                    float bmi = UtilTooth.myround(UtilTooth.countBMI2(record.getRweight(),
                            (UtilConstants.CURRENT_USER.getBheigth() / 100)));
                    record.setRbmi(bmi);
                    record.setSbmi(record.getRbmi() + "");
                }
            }
            LogUtil.e("***处理数据对比-->" + record.toString());
            LogUtil.e("***当前的秤类型-->" + record.getScaleType() + ",recod.getUgroup():" + record.getUgroup());
            if (!UtilConstants.KITCHEN_SCALE.equals(record.getScaleType())) {
                lastRecod = recordService.findLastRecordsByScaleTypeAndUser(record.getScaleType(), UtilConstants.SELECT_USER + "");
            }
            LogUtil.e("***lastRecord-->" + lastRecod);
            if (null != lastRecod) {
                record.setCompareRecord((UtilTooth.myround(record.getRweight() - lastRecod.getRweight())) + "");
                record.setComBodyWater(UtilTooth.myround(record.getComBodyWater() - lastRecod.getRbodywater()));
                record.setComBodyFat(UtilTooth.myround(record.getRbodyfat() - lastRecod.getRbodyfat()));
                record.setComBone(UtilTooth.myround(record.getRbone() - lastRecod.getRbone()));
                record.setComBmi(UtilTooth.myround(record.getRbmi() - lastRecod.getRbmi()));
                record.setComVisFat(UtilTooth.myround(record.getRvisceralfat() - lastRecod.getRvisceralfat()));
                record.setComBmr(UtilTooth.myround(record.getRbmr() - lastRecod.getRbmr()));
                record.setComMuscle(UtilTooth.myround(record.getRmuscle() - lastRecod.getRmuscle()));
                record.setComAge(UtilTooth.myround(record.getBodyAge() - lastRecod.getBodyAge()));
            } else {
                record.setCompareRecord("0.0");
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return record;
    }

    public static void setProcessWeightData6(Context context, String readMessage, TextView textView5, TextView textView1, String unit1, boolean isbaby) {
        if (TextUtils.isEmpty(readMessage) && readMessage.length() < 22) {
            return;
        }
        String unit = readMessage.substring(16, 18);
        float weight = StringUtils.hexToTen(readMessage.substring(8, 10) + readMessage.substring(6, 8)) * 0.01f;
        if (unit.equals("00")) {//kg
            textView5.setText(UtilTooth.keep1Point(weight) + "", null);
            textView1.setText("kg");
        } else if (unit.equals("01")) {//lb
            textView1.setText("lb");
            if (isbaby) {
                textView5.setText(UtilTooth.lbozToString(context, weight), null);

            } else {
                textView5.setText(UtilTooth.kgToLB_ForFatScale(weight), null);
                //textView1.setText(UtilTooth.kgToLB_ForFatScale(weight), null);
            }
        } /*else if (unit.equals("st") || unit.equals("st:lb")) {//st
            textView1.setText("st");
            if (isbaby) {
                textView5.setText(UtilTooth.lbozToString(context, weight), null);
               // textView1.setText(UtilTooth.lbozToString(context, weight), null);
            } else {
               *//* String[] tempS = UtilTooth.kgToStLbForScaleFat2(weight);
                textView5.setText(tempS[0], tempS[1]);
               // textView1.setText(tempS[0], tempS[1]);*//*
            }
        } else if (unit.equals("斤")) {//斤
            textView5.setText(UtilTooth.keep1Point(weight) + "", null);
           // textView1.setText(UtilTooth.keep1Point(weight) + "", null);
        } else if (unit.equals("g")) {//g
            textView1.setText("g");
            textView5.setText(UtilTooth.keep1Point(weight) + "", null);
           // textView1.setText(UtilTooth.keep1Point(weight) + "", null);
        } else if (unit.equals("lb:oz")) {//lb:oz
            textView1.setText("lb:oz");
            textView5.setText(UtilTooth.keep1Point(weight) + "", null);
           // textView1.setText(UtilTooth.keep1Point(weight) + "", null);
        } else if (unit.equals("oz")) {//oz
            textView5.setText(UtilTooth.keep1Point(weight) + "", null);
            //textView1.setText(UtilTooth.keep1Point(weight) + "", null);
        } else if (unit.equals("ml(water)")) {//ml(water)
            textView5.setText(UtilTooth.keep1Point(weight) + "", null);
           // textView1.setText(UtilTooth.keep1Point(weight) + "", null);
        } else if (unit.equals("ml(milk)")) {//ml(milk)
            textView5.setText(UtilTooth.keep1Point(weight) + "", null);
            //textView1.setText(UtilTooth.keep1Point(weight) + "", null);
        } else {
            textView1.setText("kg");
            textView5.setText(UtilTooth.keep1Point(weight) + "", null);
            //textView1.setText(UtilTooth.keep1Point(weight) + "", null);
        }*/
    }


    /**
     * 解析报文
     */
    public static Records parseMeaageForBaby(RecordService recordService, String readMessage) {
        Records recod = new Records();
        try {
            recod.setScaleType(readMessage.substring(0, 2));
            recod.setUgroup("P" + StringUtils.hexToTen(readMessage.substring(3, 4)) + "");
            recod.setLevel(StringUtils.hexToTen(readMessage.substring(2, 3)) + "");
            String biary = StringUtils.hexToBirary(readMessage.substring(4, 6));
            if (biary.length() < 8) {
                for (int i = biary.length(); i < 8; i++) {
                    biary = "0" + biary;
                }
            }
            recod.setSex(biary.substring(0, 1));
            recod.setsAge(StringUtils.binaryToTen(biary.substring(1)) + "");
            recod.setsHeight(StringUtils.hexToTen(readMessage.substring(6, 8)) + "");
            int weight = StringUtils.hexToTen(readMessage.substring(8, 12));
            recod.setSweight((weight * 0.1) + "");
            String unit = readMessage.substring(12, 14);
            recod.setSbodyfat((StringUtils.hexToTen(readMessage.substring(12, 16)) * 0.1) + "");
            recod.setSbone((StringUtils.hexToTen(readMessage.substring(16, 18)) * 0.1) + "");
            //recod.setSbone(getBone(StringUtils.hexToTen(readMessage.substring(16, 18)), StringUtils.hexToTen(readMessage.substring(8, 12))));
            recod.setSmuscle((StringUtils.hexToTen(readMessage.substring(18, 22)) * 0.1) + "");
            recod.setSvisceralfat(StringUtils.hexToTen(readMessage.substring(22, 24)) + "");
            recod.setSbodywater((StringUtils.hexToTen(readMessage.substring(24, 28)) * 0.1) + "");
            recod.setSbmr((StringUtils.hexToTen(readMessage.substring(28, 32)) * 1) + "");

            if (readMessage.length() > 32)
                recod.setBodyAge(StringUtils.hexToTen(readMessage.substring(32, 34)) * 1);

            recod.setRbmr(StringUtils.isNumber(recod.getSbmr()) == true ? (Integer.parseInt(recod.getSbmr()) / 1) : 0);
            recod.setRbodyfat(StringUtils.isNumber(recod.getSbodyfat()) == true ? Float.parseFloat(recod.getSbodyfat()) : 0);
            recod.setRbodywater(StringUtils.isNumber(recod.getSbodywater()) == true ? Float.parseFloat(recod.getSbodywater()) : 0);
            recod.setRbone(StringUtils.isNumber(recod.getSbone()) == true ? Float.parseFloat(recod.getSbone()) : 0);
            recod.setRmuscle(StringUtils.isNumber(recod.getSmuscle()) == true ? Float.parseFloat(recod.getSmuscle()) : 0);
            recod.setRvisceralfat(StringUtils.isNumber(recod.getSvisceralfat()) == true ? (Integer.parseInt(recod.getSvisceralfat()) / 1) : 0);
            recod.setRweight(StringUtils.isNumber(recod.getSweight()) == true ? Float.parseFloat(recod.getSweight()) : 0);
            if (UtilConstants.BABY_SCALE.equals(recod.getScaleType())) {
                recod.setUnitType(StringUtils.hexToTen(unit));
                recod.setRweight(UtilTooth.myround2((float) (recod.getRweight() * 0.1)));
            } else if (UtilConstants.KITCHEN_SCALE.equals(recod.getScaleType())) { //单位是g
                recod.setSweight(weight + "");
                recod.setRweight(Float.parseFloat(recod.getSweight()));
                recod.setUnitType(StringUtils.hexToTen(readMessage.substring(12, 14)));
            } else {
                recod.setRweight(UtilTooth.myround((float) (recod.getRweight() * 0.1)));
            }
            if (UtilConstants.KITCHEN_SCALE.equals(recod.getScaleType())) {
                recod.setSbmi("0.0");
                recod.setRbmi(0.0f);
            } else {
                if (StringUtils.isNumber(recod.getsHeight()) == true && !"0".equals(recod.getsHeight())) {
                    recod.setSbmi(UtilTooth.countBMI(recod.getRweight(), (Float.parseFloat(recod.getsHeight())) / 100));
                }
                if (StringUtils.isNumber(recod.getSbmi())) {
                    recod.setRbmi(UtilTooth.myround(Float.parseFloat(recod.getSbmi())));
                }
            }
            if (!UtilConstants.KITCHEN_SCALE.equals(recod.getScaleType())) {
                lastRecod = recordService.findLastRecordsByScaleType(recod.getScaleType(), recod.getUgroup());
            }
            if (null != lastRecod) {
                recod.setCompareRecord((UtilTooth.myround(recod.getRweight() - lastRecod.getRweight())) + "");
                recod.setComBodyWater(UtilTooth.myround(recod.getComBodyWater() - lastRecod.getRbodywater()));
                recod.setComBodyFat(UtilTooth.myround(recod.getRbodyfat() - lastRecod.getRbodyfat()));
                recod.setComBone(UtilTooth.myround(recod.getRbone() - lastRecod.getRbone()));
                recod.setComBmi(UtilTooth.myround(recod.getRbmi() - lastRecod.getRbmi()));
                recod.setComVisFat(UtilTooth.myround(recod.getRvisceralfat() - lastRecod.getRvisceralfat()));
                recod.setComBmr(UtilTooth.myround(recod.getRbmr() - lastRecod.getRbmr()));
                recod.setComMuscle(UtilTooth.myround(recod.getRmuscle() - lastRecod.getRmuscle()));
                recod.setComAge(UtilTooth.myround(recod.getBodyAge() - lastRecod.getBodyAge()));
            } else {
                recod.setCompareRecord("0.0");
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return recod;
    }

    public static Records parseZuKangMeaage(RecordService recordService, String readMessage, UserModel user) {
        Log.e("test", "解析数据1：" + readMessage);
        LogUtils.i(TAG, "============user:" + user.toString());
        Records recod = null;
        if (TextUtils.isEmpty(readMessage) && readMessage.length() < 40) {
            return null;
        }
        int age = 0;
        double height = 0;
        double weight = 0;
        int impedance = 0;
        int sex = 0;
        int level = 0;

        try {
            recod = new Records();
            recod.setUseId(user.getId());
            recod.setScaleType(user.getScaleType());
            recod.setUgroup(user.getGroup());
            recod.setLevel(user.getLevel());

            weight = StringUtils.hexToTen(readMessage.substring(24, 26) + readMessage.substring(22, 24)) * 0.01d;
            height = user.getBheigth();
            if (!TextUtils.isEmpty(user.getSex())) sex = Integer.parseInt(user.getSex());
            if (!TextUtils.isEmpty(user.getLevel())) level = Integer.parseInt(user.getLevel());
            impedance = StringUtils.hexToTen(readMessage.substring(34, 36) + readMessage.substring(32, 34) + readMessage.substring(30, 32));
            age = user.getAgeYear();

            recod.setSex(sex + "");
            recod.setsAge(age + "");
            recod.setsHeight(String.valueOf(height));
            recod.setSweight(weight + "");
            recod.setRweight((float) weight);
            recod.setRecordTime(UtilTooth.dateTimeChange(new Date()));
            HTBodyfatGeneral bodyfat = new HTBodyfatGeneral(weight, height, sex, age, level, impedance);

            //Log.e(TAG, "输入参数==>体重："+weight+"  身高:"+height+"  性别:"+sex+"  年龄:"+age+"  类型:"+level+"  阻抗:"+impedance);

            //Log.e(TAG, "计算结果==>" + bodyfat.getBodyfatParameters()+"====阻抗系数++>"+impedance);
            if (bodyfat.getBodyfatParameters() == HTDataType.ErrorNone) {
                //正常计算
                recod.setRbmi(UtilTooth.keep1Point3(bodyfat.BMI * 0.1f));
                recod.setRbmr((int) bodyfat.BMR);
                recod.setRbodyfat(UtilTooth.keep1Point3(bodyfat.bodyfatPercentage));
                recod.setRbodywater(UtilTooth.keep1Point3(bodyfat.waterPercentage));
                recod.setRbone(UtilTooth.keep1Point3(bodyfat.boneKg));
                recod.setRmuscle(UtilTooth.keep1Point3(bodyfat.muscleKg));
                recod.setRvisceralfat((int) bodyfat.VFAL);

                recod.setSbmi(UtilTooth.onePoint(recod.getRbmi()));
                recod.setSbodyfat(UtilTooth.onePoint(recod.getRbodyfat()));
                recod.setSbone(UtilTooth.onePoint(recod.getRbone()));
                recod.setSmuscle(UtilTooth.onePoint(recod.getRmuscle()));
                recod.setSvisceralfat(UtilTooth.onePoint(recod.getRvisceralfat()));
                recod.setSbodywater(UtilTooth.onePoint(recod.getRbodywater()));
                recod.setSbmr(UtilTooth.onePoint(recod.getRbmr()));
                float bmi = UtilTooth.countBMI2(recod.getRweight(), (user.getBheigth() / 100));
              /*  //计算身体年龄
                String stats = readMessage.substring(18, 20);
               LogUtils.i(TAG,"========计算身体年龄===== readMessage.substring(18, 20):"+stats);
                if(!"36".equals(stats)&&readMessage.length()==22){
                    LogUtils.i(TAG,"调用计算身体年龄");
                    recod.setBodyAge(UtilTooth.getPhysicAge(bmi,user.getAgeYear()));
                }else{
                    LogUtils.i(TAG,"不调用计算身体年龄");
                }*/


                Log.i(TAG, "阻抗:" + bodyfat.ZTwoLegs +
                        "Ω  BMI:" + String.format("%.1f", bodyfat.BMI) +
                        "  BMR:" + (int) bodyfat.BMR +
                        "  内脏脂肪:" + (int) bodyfat.VFAL +
                        "  骨量:" + String.format("%.1fkg", bodyfat.boneKg) +
                        "  脂肪率:" + String.format("%.1f%%", bodyfat.bodyfatPercentage) +
                        "  水分:" + String.format("%.1f%%", bodyfat.waterPercentage) +
                        "  肌肉:" + String.format("%.1fkg", bodyfat.muscleKg) + "\r\n");
            } else {
                recod.setRbmi(UtilTooth.myround(UtilTooth.countBMI2(recod.getRweight(), (Float.parseFloat(recod.getsHeight())) / 100)));
                recod.setSbmi(UtilTooth.onePoint(recod.getRbmi()));
                Log.e(TAG, "输入数据有误==>" + bodyfat.toString());
            }
            lastRecod = recordService.findLastRecordsByScaleType(
                    recod.getScaleType(), recod.getUgroup());
            if (null != lastRecod) {
                recod.setCompareRecord((UtilTooth.myround(recod
                        .getRweight() - lastRecod.getRweight()))
                        + "");
                recod.setComBodyWater(UtilTooth.myround(recod.getComBodyWater() - lastRecod.getRbodywater()));
                recod.setComBodyFat(UtilTooth.myround(recod.getRbodyfat() - lastRecod.getRbodyfat()));
                recod.setComBone(UtilTooth.myround(recod.getRbone() - lastRecod.getRbone()));
                recod.setComBmi(UtilTooth.myround(recod.getRbmi() - lastRecod.getRbmi()));
                recod.setComVisFat(UtilTooth.myround(recod.getRvisceralfat() - lastRecod.getRvisceralfat()));
                recod.setComBmr(UtilTooth.myround(recod.getRbmr() - lastRecod.getRbmr()));
                recod.setComMuscle(UtilTooth.myround(recod.getRmuscle() - lastRecod.getRmuscle()));
                recod.setComAge(UtilTooth.myround(recod.getBodyAge() - lastRecod.getBodyAge()));
            } else {
                recod.setCompareRecord("0.0");
            }
            //recordService.saveOneRecord(recod);
        } catch (Exception e) {
            Log.e(TAG, "解析数据异常==>" + e.getMessage());
        }
        return recod;
    }

    /**
     * 是否是锁定数据
     *
     * @param readMessage
     * @return
     */
    public static boolean isLockData(String readMessage) {
        if (TextUtils.isEmpty(readMessage) && readMessage.length() < 22) {
            return false;
        }
        String stats = readMessage.substring(18, 20);
        if ("00".equals(stats)) {
            return true;
        } else {
            return false;
        }
    }

    /**
     * 获取体重数据
     *
     * @param readMessage
     * @return
     */
    public static float getWeightData(String readMessage) {
        if (TextUtils.isEmpty(readMessage) && readMessage.length() < 22) {
            return 0;
        }
        Records recod = new Records();
        String unit = readMessage.substring(16, 18);
        float weight = StringUtils.hexToTen(readMessage.substring(8, 10) + readMessage.substring(6, 8)) * 0.01f;
        if (unit.equals("00")) {//kg

        } else if (unit.equals("01")) {//lb

        } else if (unit.equals("02")) {//st

        } else if (unit.equals("03")) {//斤

        } else if (unit.equals("04")) {//g

        } else if (unit.equals("05")) {//lb:oz

        } else if (unit.equals("06")) {//oz

        } else if (unit.equals("07")) {//ml(water)

        } else if (unit.equals("08")) {//ml(milk)

        } else {

        }
        return weight;
    }

    public static void setProcessWeightAutoData0(Context context, String readMessage, TextView textView5, TextView textView1, String unit, boolean isbaby) {
        Log.e("setProcessWeightData1", "***readMessage-->"+readMessage +"***readMessage.length-->"+readMessage.length());
        if (TextUtils.isEmpty(readMessage) && readMessage.length() < 22) {
            return;
        }
        float weight = StringUtils.hexToTen(readMessage.substring(8, 10) + readMessage.substring(10, 12)) * 0.1f;
        if (unit.equals("kg")) {//kg
            textView5.setText(UtilTooth.keep1Point(weight) + "", null);
            textView1.setText("kg");
        } else if (unit.equals("lb")) {//lb
            if (isbaby) {
                textView5.setText(UtilTooth.lbozToString(context, weight), null);
                textView1.setVisibility(View.GONE);
            } else {
                textView5.setText(UtilTooth.kgToLB_ForFatScale(weight), null);
                textView1.setText("lb");
            }
        }
    }

    public static void setProcessWeightData0(Context context, String readMessage, MyTextView5 textView5, TextView textView1, String unit, boolean isbaby) {
        Log.e("setProcessWeightData1", "***readMessage-->"+readMessage +"***readMessage.length-->"+readMessage.length());
        if (TextUtils.isEmpty(readMessage) && readMessage.length() < 22) {
            return;
        }
        float weight = StringUtils.hexToTen(readMessage.substring(8, 10) + readMessage.substring(10, 12)) * 0.1f;
        if (unit.equals("kg")) {//kg
            textView5.setTexts(UtilTooth.keep1Point(weight) + "", null);
            textView1.setText("kg");
        } else if (unit.equals("lb")) {//lb
            if (isbaby) {
                textView5.setTexts(UtilTooth.lbozToString(context, weight), null);
                textView1.setVisibility(View.GONE);
            } else {
                textView5.setTexts(UtilTooth.kgToLB_ForFatScale(weight), null);
                textView1.setText("lb");
            }
        }
    }

    public static void setProcessWeightData0(Context context, String readMessage, MyTextView5 textView5, MyTextView5 textView1, String unit, boolean isbaby) {
        Log.e("setProcessWeightData1", "***readMessage-->"+readMessage +"***readMessage.length-->"+readMessage.length());
        if (TextUtils.isEmpty(readMessage) && readMessage.length() < 22) {
            return;
        }
        float weight = StringUtils.hexToTen(readMessage.substring(8, 10) + readMessage.substring(10, 12)) * 0.1f;
        if (unit.equals("kg")) {//kg
            textView5.setTexts(UtilTooth.keep1Point(weight) + "", null);
            textView1.setTexts(UtilTooth.keep1Point(weight) + "", null);
        } else if (unit.equals("lb")) {//lb
            if (isbaby) {
                textView5.setTexts(UtilTooth.lbozToString(context, weight), null);
                textView1.setTexts(UtilTooth.lbozToString(context, weight), null);
            } else {
                textView5.setTexts(UtilTooth.kgToLB_ForFatScale(weight), null);
                textView1.setTexts(UtilTooth.kgToLB_ForFatScale(weight), null);
            }
        } else if (unit.equals("st") || unit.equals("st:lb")) {//st
            if (isbaby) {
                textView5.setTexts(UtilTooth.lbozToString(context, weight), null);
                textView1.setTexts(UtilTooth.lbozToString(context, weight), null);
            } else {
                String[] tempS = UtilTooth.kgToStLbForScaleFat2(weight);
                textView5.setTexts(tempS[0], tempS[1]);
                textView1.setTexts(tempS[0], tempS[1]);
            }
        } else if (unit.equals("03")) {//斤
            textView5.setTexts(UtilTooth.keep1Point(weight) + "", null);
            textView1.setTexts(UtilTooth.keep1Point(weight) + "", null);
        } else if (unit.equals("g")) {//g
            textView5.setTexts(UtilTooth.keep1Point(weight) + "", null);
            textView1.setTexts(UtilTooth.keep1Point(weight) + "", null);
        } else if (unit.equals("lb:oz")) {//lb:oz
            textView5.setTexts(UtilTooth.keep1Point(weight) + "", null);
            textView1.setTexts(UtilTooth.keep1Point(weight) + "", null);
        } else if (unit.equals("oz")) {//oz
            textView5.setTexts(UtilTooth.keep1Point(weight) + "", null);
            textView1.setTexts(UtilTooth.keep1Point(weight) + "", null);
        } else if (unit.equals("ml(water)")) {//ml(water)
            textView5.setTexts(UtilTooth.keep1Point(weight) + "", null);
            textView1.setTexts(UtilTooth.keep1Point(weight) + "", null);
        } else if (unit.equals("ml(milk)")) {//ml(milk)
            textView5.setTexts(UtilTooth.keep1Point(weight) + "", null);
            textView1.setTexts(UtilTooth.keep1Point(weight) + "", null);
        } else {
            textView5.setTexts(UtilTooth.keep1Point(weight) + "", null);
            textView1.setTexts(UtilTooth.keep1Point(weight) + "", null);
        }
    }

    public static void setProcessWeightData(Context context, String readMessage, MyTextView5 textView5, String unit, boolean isbaby) {
        if (TextUtils.isEmpty(readMessage) && readMessage.length() < 22) {
            return;
        }
        float weight = StringUtils.hexToTen(readMessage.substring(8, 10) + readMessage.substring(6, 8)) * 0.01f;
        if (unit.equals("kg")) {//kg
            textView5.setTexts(UtilTooth.keep1Point(weight) + "", null);
        } else if (unit.equals("lb")) {//lb
            if (isbaby) {
                textView5.setTexts(UtilTooth.lbozToString(context, weight), null);
            } else {
                textView5.setTexts(UtilTooth.kgToLB_ForFatScale(weight), null);
            }
        } else if (unit.equals("st") || unit.equals("st:lb")) {//st
            if (isbaby) {
                textView5.setTexts(UtilTooth.lbozToString(context, weight), null);
            } else {
                String[] tempS = UtilTooth.kgToStLbForScaleFat2(weight);
                textView5.setTexts(tempS[0], tempS[1]);
            }
        } else if (unit.equals("斤")) {//斤
            textView5.setTexts(UtilTooth.keep1Point(weight) + "", null);
        } else if (unit.equals("g")) {//g
            textView5.setTexts(UtilTooth.keep1Point(weight) + "", null);
        } else if (unit.equals("lb:oz")) {//lb:oz
            textView5.setTexts(UtilTooth.keep1Point(weight) + "", null);
        } else if (unit.equals("oz")) {//oz
            textView5.setTexts(UtilTooth.keep1Point(weight) + "", null);
        } else if (unit.equals("ml(water)")) {//ml(water)
            textView5.setTexts(UtilTooth.keep1Point(weight) + "", null);
        } else if (unit.equals("ml(milk)")) {//ml(milk)
            textView5.setTexts(UtilTooth.keep1Point(weight) + "", null);
        } else {
            textView5.setTexts(UtilTooth.keep1Point(weight) + "", null);
        }
    }

    public static void setProcessWeightData1(Context context, String readMessage, MyTextView5 textView5, MyTextView5 textView1, String unit, boolean isbaby) {
        if (TextUtils.isEmpty(readMessage) && readMessage.length() < 22) {
            return;
        }
        float weight = StringUtils.hexToTen(readMessage.substring(8, 10) + readMessage.substring(6, 8)) * 0.01f;
        if (unit.equals("kg")) {//kg
            textView5.setTexts(UtilTooth.keep1Point(weight) + "", null);
            textView1.setTexts(UtilTooth.keep1Point(weight) + "", null);
        } else if (unit.equals("lb")) {//lb
            if (isbaby) {
                textView5.setTexts(UtilTooth.lbozToString(context, weight), null);
                textView1.setTexts(UtilTooth.lbozToString(context, weight), null);
            } else {
                textView5.setTexts(UtilTooth.kgToLB_ForFatScale(weight), null);
                textView1.setTexts(UtilTooth.kgToLB_ForFatScale(weight), null);
            }
        } else if (unit.equals("st") || unit.equals("st:lb")) {//st
            if (isbaby) {
                textView5.setTexts(UtilTooth.lbozToString(context, weight), null);
                textView1.setTexts(UtilTooth.lbozToString(context, weight), null);
            } else {
                String[] tempS = UtilTooth.kgToStLbForScaleFat2(weight);
                textView5.setTexts(tempS[0], tempS[1]);
                textView1.setTexts(tempS[0], tempS[1]);
            }
        } else if (unit.equals("03")) {//斤
            textView5.setTexts(UtilTooth.keep1Point(weight) + "", null);
            textView1.setTexts(UtilTooth.keep1Point(weight) + "", null);
        } else if (unit.equals("g")) {//g
            textView5.setTexts(UtilTooth.keep1Point(weight) + "", null);
            textView1.setTexts(UtilTooth.keep1Point(weight) + "", null);
        } else if (unit.equals("lb:oz")) {//lb:oz
            textView5.setTexts(UtilTooth.keep1Point(weight) + "", null);
            textView1.setTexts(UtilTooth.keep1Point(weight) + "", null);
        } else if (unit.equals("oz")) {//oz
            textView5.setTexts(UtilTooth.keep1Point(weight) + "", null);
            textView1.setTexts(UtilTooth.keep1Point(weight) + "", null);
        } else if (unit.equals("ml(water)")) {//ml(water)
            textView5.setTexts(UtilTooth.keep1Point(weight) + "", null);
            textView1.setTexts(UtilTooth.keep1Point(weight) + "", null);
        } else if (unit.equals("ml(milk)")) {//ml(milk)
            textView5.setTexts(UtilTooth.keep1Point(weight) + "", null);
            textView1.setTexts(UtilTooth.keep1Point(weight) + "", null);
        } else {
            textView5.setTexts(UtilTooth.keep1Point(weight) + "", null);
            textView1.setTexts(UtilTooth.keep1Point(weight) + "", null);
        }
    }

    public static void setProcessWeightData2(Context context, String readMessage, TextView textView5, TextView textView1, String unit1, boolean isbaby) {
        if (TextUtils.isEmpty(readMessage) && readMessage.length() < 22) {
            return;
        }
         String unit = readMessage.substring(16, 18);
        float weight = StringUtils.hexToTen(readMessage.substring(8, 10) + readMessage.substring(6, 8)) * 0.01f;
        if (unit.equals("00")) {//kg
            textView5.setText(UtilTooth.keep1Point(weight) + "", null);
            textView1.setText("kg");
        } else if (unit.equals("01")) {//lb
            textView1.setText("lb");
            if (isbaby) {
                textView5.setText(UtilTooth.lbozToString(context, weight), null);

            } else {
                textView5.setText(UtilTooth.kgToLB_ForFatScale(weight), null);
                //textView1.setText(UtilTooth.kgToLB_ForFatScale(weight), null);
            }
        } /*else if (unit.equals("st") || unit.equals("st:lb")) {//st
            textView1.setText("st");
            if (isbaby) {
                textView5.setText(UtilTooth.lbozToString(context, weight), null);
               // textView1.setText(UtilTooth.lbozToString(context, weight), null);
            } else {
               *//* String[] tempS = UtilTooth.kgToStLbForScaleFat2(weight);
                textView5.setText(tempS[0], tempS[1]);
               // textView1.setText(tempS[0], tempS[1]);*//*
            }
        } else if (unit.equals("斤")) {//斤
            textView5.setText(UtilTooth.keep1Point(weight) + "", null);
           // textView1.setText(UtilTooth.keep1Point(weight) + "", null);
        } else if (unit.equals("g")) {//g
            textView1.setText("g");
            textView5.setText(UtilTooth.keep1Point(weight) + "", null);
           // textView1.setText(UtilTooth.keep1Point(weight) + "", null);
        } else if (unit.equals("lb:oz")) {//lb:oz
            textView1.setText("lb:oz");
            textView5.setText(UtilTooth.keep1Point(weight) + "", null);
           // textView1.setText(UtilTooth.keep1Point(weight) + "", null);
        } else if (unit.equals("oz")) {//oz
            textView5.setText(UtilTooth.keep1Point(weight) + "", null);
            //textView1.setText(UtilTooth.keep1Point(weight) + "", null);
        } else if (unit.equals("ml(water)")) {//ml(water)
            textView5.setText(UtilTooth.keep1Point(weight) + "", null);
           // textView1.setText(UtilTooth.keep1Point(weight) + "", null);
        } else if (unit.equals("ml(milk)")) {//ml(milk)
            textView5.setText(UtilTooth.keep1Point(weight) + "", null);
            //textView1.setText(UtilTooth.keep1Point(weight) + "", null);
        } else {
            textView1.setText("kg");
            textView5.setText(UtilTooth.keep1Point(weight) + "", null);
            //textView1.setText(UtilTooth.keep1Point(weight) + "", null);
        }*/
    }

    /**
     * DL Scale秤信息
     *
     * @param recordService
     * @param readMessage
     * @param user
     * @return CF 88 13 00 14 00 00 00 00 00 40
     */
    public static Records parseDLScaleMessage(RecordService recordService, String readMessage, UserModel user) {
        LogUtil.e("****解析数据**readMessage-->" + readMessage);
        Records recod = null;
        if (TextUtils.isEmpty(readMessage) && readMessage.length() < 22) {
            return null;
        }
        int age = 0;
        double height = 0;
        double weight = 0;
        int impedance = 0;
        int sex = 0;
        int level = 0;
        recod = new Records();
        recod.setUseId(user.getId());
        recod.setUgroup(user.getGroup());
        recod.setLevel(user.getLevel());

        String scaleType = readMessage.substring(0, 2);
        impedance = (StringUtils.hexToTen(readMessage.substring(14, 16)
                + readMessage.substring(12, 14) + readMessage.substring(10, 12)));
        //String unit = readMessage.substring(16, 18);
        weight = StringUtils.hexToTen(readMessage.substring(8, 10) + readMessage.substring(6, 8)) * 0.01d;
        height = user.getBheigth();
        if (!TextUtils.isEmpty(user.getSex())) sex = Integer.parseInt(user.getSex());
        if (!TextUtils.isEmpty(user.getLevel())) level = Integer.parseInt(user.getLevel());
        String unit = readMessage.substring(16, 18);
        age = user.getAgeYear();

        recod.setScaleType(scaleType);
        recod.setSex(sex + "");
        recod.setsAge(age + "");
        recod.setsHeight(String.valueOf(height));
        recod.setSweight(weight + "");
        recod.setRweight((float) weight);
        System.currentTimeMillis();
        recod.setRecordTime(UtilTooth.dateTimeChange(new Date()));
        if (unit.equals("00")) {//kg
            recod.setUnitType(0);
        } else if (unit.equals("01")) {//lb
            recod.setUnitType(1);
        } else if (unit.equals("02")) {//st
            recod.setUnitType(2);
        } else if (unit.equals("03")) {//斤
            recod.setUnitType(3);
        } else if (unit.equals("04")) {//g
            recod.setUnitType(4);
        } else if (unit.equals("05")) {//lb:oz
            recod.setUnitType(5);
        } else if (unit.equals("06")) {//oz
            recod.setUnitType(6);
        } else if (unit.equals("07")) {//ml(water)
            recod.setUnitType(7);
        } else if (unit.equals("08")) {//ml(milk)
            recod.setUnitType(8);
        } else {
            recod.setUnitType(0);
        }
        try {
            if (readMessage.startsWith(UtilConstants.BATHROOM_SCALE)) {
                float bmi = UtilTooth.myround(UtilTooth.countBMI2(recod.getRweight(), (user.getBheigth() / 100)));
                recod.setRbmi(bmi);
                recod.setSbmi(recod.getRbmi() + "");
            } else {
                HTBodyfatGeneral bodyfat = new HTBodyfatGeneral(weight, height, sex, age, level, impedance);
                LogUtil.e("***输入参数==>体重：" + weight + "  身高:" + height
                        + "  性别:" + sex + "  年龄:" + age + "  类型:" + level + "  阻抗:" + impedance);
                LogUtil.e("***计算结果==>" + bodyfat.getBodyfatParameters() + "====阻抗系数++>" + impedance);
                if (bodyfat.getBodyfatParameters() == HTDataType.ErrorNone) {
                    //正常计算
                    recod.setRbmi(UtilTooth.keep1Point3(bodyfat.BMI));
                    recod.setRbmr((int) bodyfat.BMR);
                    recod.setRbodyfat(UtilTooth.keep1Point3(bodyfat.bodyfatPercentage));
                    recod.setRbodywater(UtilTooth.keep1Point3(bodyfat.waterPercentage));
                    recod.setRbone(UtilTooth.keep1Point3(bodyfat.boneKg));
                    recod.setRmuscle(UtilTooth.keep1Point3(bodyfat.muscleKg));
                    recod.setRvisceralfat((int) bodyfat.VFAL);
                    recod.setSbmi(UtilTooth.onePoint(recod.getRbmi()));
                    recod.setSbodyfat(UtilTooth.onePoint(recod.getRbodyfat()));
                    recod.setSbone(UtilTooth.onePoint(recod.getRbone()));
                    recod.setSmuscle(UtilTooth.onePoint(recod.getRmuscle()));
                    recod.setSvisceralfat(UtilTooth.onePoint(recod.getRvisceralfat()));
                    recod.setSbodywater(UtilTooth.onePoint(recod.getRbodywater()));
                    recod.setSbmr(UtilTooth.onePoint(recod.getRbmr()));
                    String ageFlag = readMessage.substring(18, 20);
                    LogUtil.e("****ageFlag-->" + ageFlag);
                    LogUtil.e("****阻抗:" + bodyfat.ZTwoLegs +
                            "Ω  BMI:" + String.format("%.1f", bodyfat.BMI) +
                            "  BMR:" + (int) bodyfat.BMR +
                            "  内脏脂肪:" + (int) bodyfat.VFAL +
                            "  骨量:" + String.format("%.1fkg", bodyfat.boneKg) +
                            "  脂肪率:" + String.format("%.1f%%", bodyfat.bodyfatPercentage) +
                            "  水分:" + String.format("%.1f%%", bodyfat.waterPercentage) +
                            "  肌肉:" + String.format("%.1fkg", bodyfat.muscleKg) + "\r\n");
                } else {
                    float bmi = UtilTooth.myround(UtilTooth.countBMI2(recod.getRweight(), (user.getBheigth() / 100)));
                    recod.setRbmi(bmi);
                    recod.setSbmi(recod.getRbmi() + "");
                    LogUtil.e("****输入数据有误==>" + bodyfat.toString());
                }
            }
            LogUtil.e("****根据类型来对比数据：" + recod.toString());
            lastRecod = recordService.findLastRecordsByScaleType(
                    recod.getScaleType(), recod.getUgroup());
            if (null != lastRecod) {
                LogUtil.e("****得到上一组的数据：" + lastRecod.toString());
                recod.setCompareRecord((UtilTooth.myround(recod.getRweight() - lastRecod.getRweight())) + "");
                recod.setComBodyWater(UtilTooth.myround(recod.getComBodyWater() - lastRecod.getRbodywater()));
                recod.setComBodyFat(UtilTooth.myround(recod.getRbodyfat() - lastRecod.getRbodyfat()));
                recod.setComBone(UtilTooth.myround(recod.getRbone() - lastRecod.getRbone()));
                recod.setComBmi(UtilTooth.myround(recod.getRbmi() - lastRecod.getRbmi()));
                recod.setComVisFat(UtilTooth.myround(recod.getRvisceralfat() - lastRecod.getRvisceralfat()));
                recod.setComBmr(UtilTooth.myround(recod.getRbmr() - lastRecod.getRbmr()));
                recod.setComMuscle(UtilTooth.myround(recod.getRmuscle() - lastRecod.getRmuscle()));
                recod.setComAge(UtilTooth.myround(recod.getBodyAge() - lastRecod.getBodyAge()));
                LogUtil.e("****处理对比数据后：" + recod.toString());
            } else {
                recod.setCompareRecord("0.0");
            }
        } catch (Exception e) {
            LogUtil.e("****Exception e-->"+e.getMessage());
            e.printStackTrace();
        }
        return recod;
    }

    /**
     * 对接收到的营养秤数据进行处理
     * @param readMessage
     * @param user
     * @return CF 88 13 00 14 00 00 00 00 00 40
     */
    public static Records parseCAScaleMeaage(String readMessage, UserModel user) {
        Log.e("test", "解析数据：" + readMessage);
        Records recod = null;
        if (TextUtils.isEmpty(readMessage) && readMessage.length() < 22) {
            return null;
        }

        int age = 0;
        double height = 0;
        double weight = 0;
        int sex = 0;
        int level = 0;


        recod = new Records();
        recod.setUseId(user.getId());
        recod.setUgroup(user.getGroup());
        recod.setLevel(user.getLevel());
        String scaleType = readMessage.substring(0, 2);
        weight = StringUtils.hexToTen(readMessage.substring(8, 10) + readMessage.substring(6, 8));
        height = user.getBheigth();
        if (!TextUtils.isEmpty(user.getSex())) sex = Integer.parseInt(user.getSex());
        if (!TextUtils.isEmpty(user.getLevel())) level = Integer.parseInt(user.getLevel());
        String unit = readMessage.substring(16, 18);
        age = user.getAgeYear();

        recod.setScaleType(scaleType);
        recod.setSex(sex + "");
        recod.setsAge(age + "");
        recod.setsHeight(String.valueOf(height));
        recod.setSweight(weight + "");
        recod.setRweight((float) weight);
        System.currentTimeMillis();

        recod.setRecordTime(UtilTooth.dateTimeChange(new Date()));

        if (unit.equals("00")) {//kg
            recod.setUnitType(0);
        } else if (unit.equals("01")) {//lb
            recod.setUnitType(1);
        } else if (unit.equals("02")) {//st
            recod.setUnitType(2);
        } else if (unit.equals("03")) {//斤
            recod.setUnitType(3);
        } else if (unit.equals("04")) {//g
            recod.setUnitType(4);
        } else if (unit.equals("05")) {//lb:oz
            recod.setUnitType(5);
        } else if (unit.equals("06")) {//oz
            recod.setUnitType(6);
        } else if (unit.equals("07")) {//ml(water)
            recod.setUnitType(7);
        } else if (unit.equals("08")) {//ml(milk)
            recod.setUnitType(8);
        } else {
            recod.setUnitType(0);
        }
        recod.setCompareRecord("0.0");
        return recod;
    }

    /**
     * 获取骨骼
     */
    private static String getBone(int bone, int weight) {
        double bo = ((bone * 0.1) / (weight * 0.1) * 100);
        bo = bo * 0.01 * (weight * 0.1);
        return UtilTooth.myroundString(bo + "");
    }

    /**
     * 四舍五入 保留一位小数
     *
     * @param f
     * @return
     */
    public static double onePoint(double f) {
        BigDecimal b = new BigDecimal(f);
        double f1 = b.setScale(1, BigDecimal.ROUND_HALF_UP).doubleValue();
        return f1;
    }

    public static Records parseZuKangMessage(RecordService recordService, String readMessage, UserModel user) {
        LogUtils.e(TAG,"***解析数据***" + readMessage + "****user****" + user.toString());
        Records record = null;
        if (TextUtils.isEmpty(readMessage) && readMessage.length() < 40) {
            return null;
        }
        int age = 0;
        double height = 0;
        double weight = 0;
        int impedance = 0;
        int sex = 0;
        int level = 0;
        try {
            record = new Records();
            record.setUseId(user.getId());
            record.setScaleType(user.getScaleType());
            record.setUgroup(user.getGroup());
            record.setLevel(user.getLevel());
            if(user.getDanwei().equals("kg")){
                record.setUnitType(0);
            }else{
                record.setUnitType(1);
            }

            weight = StringUtils.hexToTen(readMessage.substring(24, 26) + readMessage.substring(22, 24)) * 0.01d;
            height = user.getBheigth();
            if (!TextUtils.isEmpty(user.getSex())) sex = Integer.parseInt(user.getSex());
            if (!TextUtils.isEmpty(user.getLevel())) level = Integer.parseInt(user.getLevel());
            impedance = StringUtils.hexToTen(readMessage.substring(34, 36) + readMessage.substring(32, 34) + readMessage.substring(30, 32));
            age = user.getAgeYear();
            record.setSex(sex + "");
            record.setsAge(age + "");
            record.setsHeight(String.valueOf(height));
            record.setSweight(weight + "");
            record.setRweight((float) weight);
            record.setRecordTime(UtilTooth.dateTimeChange(new Date()));
            HTBodyfatGeneral bodyfat = new HTBodyfatGeneral(weight, height, sex, age, level, impedance);
            LogUtils.e(TAG,"输入参数==>体重："+weight+"  身高:"+height+"  性别:"+sex+"  年龄:"+age+"  类型:"+level+"  阻抗:"+impedance);
            LogUtils.e(TAG,"计算结果==>" + bodyfat.getBodyfatParameters()+"====阻抗系数++>"+impedance);
            if (bodyfat.getBodyfatParameters() == HTDataType.ErrorNone) {
                //正常计算
                record.setRbmi(UtilTooth.keep1Point3(bodyfat.BMI * 0.1f));
                record.setRbmr((int) bodyfat.BMR);
                record.setRbodyfat(UtilTooth.keep1Point3(bodyfat.bodyfatPercentage));
                record.setRbodywater(UtilTooth.keep1Point3(bodyfat.waterPercentage));
                record.setRbone(UtilTooth.keep1Point3(bodyfat.boneKg));
                record.setRmuscle(UtilTooth.keep1Point3(bodyfat.muscleKg));
                record.setRvisceralfat((int) bodyfat.VFAL);
                record.setSbmi(UtilTooth.onePoint(record.getRbmi()));
                record.setSbodyfat(UtilTooth.onePoint(record.getRbodyfat()));
                record.setSbone(UtilTooth.onePoint(record.getRbone()));
                record.setSmuscle(UtilTooth.onePoint(record.getRmuscle()));
                record.setSvisceralfat(UtilTooth.onePoint(record.getRvisceralfat()));
                record.setSbodywater(UtilTooth.onePoint(record.getRbodywater()));
                record.setSbmr(UtilTooth.onePoint(record.getRbmr()));
                float bmi = UtilTooth.countBMI2(record.getRweight(), (user.getBheigth() / 100));
                LogUtils.e(TAG,"阻抗:" + bodyfat.ZTwoLegs +
                        "Ω  BMI:" + String.format("%.1f", bodyfat.BMI) +
                        "  BMR:" + (int) bodyfat.BMR +
                        "  内脏脂肪:" + (int) bodyfat.VFAL +
                        "  骨量:" + String.format("%.1fkg", bodyfat.boneKg) +
                        "  脂肪率:" + String.format("%.1f%%", bodyfat.bodyfatPercentage) +
                        "  水分:" + String.format("%.1f%%", bodyfat.waterPercentage) +
                        "  肌肉:" + String.format("%.1fkg", bodyfat.muscleKg) + "\r\n");
            } else {
                record.setRbmi(UtilTooth.myround(UtilTooth.countBMI2(record.getRweight(), (Float.parseFloat(record.getsHeight())) / 100)));
                record.setSbmi(UtilTooth.onePoint(record.getRbmi()));
                LogUtils.e(TAG,"输入数据有误==>" + bodyfat.toString());
            }
            lastRecod = recordService.findLastRecordsByScaleType(
                    record.getScaleType(), record.getUgroup());
            if (null != lastRecod) {
                record.setCompareRecord((UtilTooth.myround(record
                        .getRweight() - lastRecod.getRweight()))
                        + "");
                record.setComBodyWater(UtilTooth.myround(record.getComBodyWater() - lastRecod.getRbodywater()));
                record.setComBodyFat(UtilTooth.myround(record.getRbodyfat() - lastRecod.getRbodyfat()));
                record.setComBone(UtilTooth.myround(record.getRbone() - lastRecod.getRbone()));
                record.setComBmi(UtilTooth.myround(record.getRbmi() - lastRecod.getRbmi()));
                record.setComVisFat(UtilTooth.myround(record.getRvisceralfat() - lastRecod.getRvisceralfat()));
                record.setComBmr(UtilTooth.myround(record.getRbmr() - lastRecod.getRbmr()));
                record.setComMuscle(UtilTooth.myround(record.getRmuscle() - lastRecod.getRmuscle()));
                record.setComAge(UtilTooth.myround(record.getBodyAge() - lastRecod.getBodyAge()));
            } else {
                record.setCompareRecord("0.0");
            }
            //recordService.saveOneRecord(recod);
        } catch (Exception e) {
            LogUtils.e(TAG,"解析数据异常==>" + e.getMessage());
        }
        return record;
    }

    /**
     * 得到当前手机的系统的语言
     *
     * @param context
     * @return
     */
    public static String getLanguage(Context context) {
        Locale locale = context.getResources().getConfiguration().locale;
        String language = locale.getLanguage();
        Log.d(TAG, "==============得到当前手机的系统的语言：" + language);
        return language;
    }

}
